package bean;

import java.io.IOException;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import dao.ClientDAO;
import dao.JPA;
import entity.Client;

@ManagedBean
@ViewScoped
public class ViewClientProfileBean {

	@Inject @JPA
	private ClientDAO clientDAO;
	private Client client = new Client();
	
	@PostConstruct
	private void init() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> params = context.getExternalContext().getRequestParameterMap();		
		client = clientDAO.getEntityById(Integer.valueOf(params.get("clientId")));
	}
	
	public void deleteProfile() {
		clientDAO.remove(client);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Client profile deleted", "Client profile deleted"));
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("findClient.xhtml");
		} catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public Client getClient() {
		return client;
	}
	
}